$(function () {
    showCondition(true, 0, 0, 1, []);
    var drg = $("#tabContent > .tab-pane.active").data();
    if (drg.value == "undefined") {
        paramTabPage[tabId].depCode = $("#depCode").val();
        paramTabPage[tabId].wardCode = $("#wardCode").val();
    } else {
        paramTabPage[tabId] = drg;
    }
    var scoreQuota = [{
        "quotaCode": "TotalScore",
        "quotaName": "综合得分"
    }, {
        "quotaCode": "CapacityScore",
        "quotaName": "产能得分"
    }, {
        "quotaCode": "EfficiencyScore",
        "quotaName": "效率得分"
    }, {
        "quotaCode": "QualityScore",
        "quotaName": "安全得分"
    }];

    paramTabPage[tabId].drg = perfQuotaDrg;
    paramTabPage[tabId].reg = perfQuotaReg;

    // 根据医师配置展示医师信息
    var layer = sysConfig.DOCTOR_SET;
    if (layer.indexOf("1") == -1) {
        $('#' + tabId + ' #tips').css("display", "block");
        return;
    }
    levels = layer.split(",");
    if (levels[0] == 1) {
        getDoctorData('protomedicus', scoreQuota, perfQuotaDrg, perfQuotaReg, totalQuota, '主任医师');
    }
    if (levels[1] == 1) {
        getDoctorData('clinic', scoreQuota, perfQuotaDrg, perfQuotaReg, totalQuota, '主诊组');
    }
    if (levels[2] == 1) {
        getDoctorData('resident', scoreQuota, perfQuotaDrg, perfQuotaReg, totalQuota, '住院医师');
    }
    if (levels[3] == 1) {
        getDoctorData('physician', scoreQuota, perfQuotaDrg, perfQuotaReg, totalQuota, '主治医师');
    }

});


/**
 * 获取医师分析数据
 * @param {} monitorType
 * @param {} scoreQuotam
 * @param {} perfQuota
 * @param {} perfQuota_1
 * @param {} totalQuota
 */
function getDoctorData(monitorType, scoreQuota, perfQuotaDrg, perfQuotaReg, totalQuota, doctorDesc) {
    $("#" + tabId + " #doctor_" + monitorType).show();
    /* 获取医师得分 */
    getDoctorScore(scoreQuota, monitorType, monitorType + "Score", doctorDesc);
    /* 雷达图 */
    drawDoctorRards(perfQuotaDrg, perfQuotaReg, monitorType);
    // 副主任医师DRGs指标综合比较汇总表
    loadTotalQuotaTable(totalQuota, monitorType);
};

/**
 *  @ Discription:获取医师
 * @Autor ：liuhongjun
 * @param totalQuota
 * @param totalQuota_1
 * @param monitorType
 * @param method
 * @param doctorLevle医师钻取层次
 * @returns any ywcDGDoctorPerf.js 2017年6月10日下午10:08:08
 */
function drawDoctorRards(perfQuotaDrg, perfQuotaReg, monitorType) {
    //绘制指标复选框
    loadQuotaCheckbox(monitorType);

    var map = drgUI.setAjaxData(tabId);
    map.monitorType = monitorType;
    var role = $("#roleName_id").val();
    // 获取医师数据
    $.post(contextPath + '/DrgsIndexCal/getDoctor.dhc', map).done(
        function (result) {
            if (result.success) {
                var data = result.data;
                if (jQuery.isEmptyObject(data)) {// 没有科室
                    $('#' + tabId + ' #' + monitorType + 'Codes ul')
                        .html("无数据");
                } else {
                    var str = "";
                    var descval = "全院均值";
                    $('#' + tabId + ' #' + monitorType + 'Codes ul').html("");
                    if (role == '科室') {//科室角色时 科室下只有一个医师时 不显示比较对象
                        if (map.deps && (data.length < 2)) {
                            avgFlag = false;
                            $('#' + tabId + ' #' + monitorType + '-well').css("display", "none");
                        }
                        descval = "授权科室均值";
                    } else {
                        descval = "全院均值";
                    }
                    $.each(data, function (n, value) {
                        var depName = value.DEP == null ? "-" : value.DEP;
                        if (n == 0) {
                            str += "<label><input  checked='checked' type='checkbox' name='"
                                + monitorType + "code' value='"
                                + value.CCODE + "' descval='"
                                + value.DDESC + "(" + value.DEP
                                + ")'/>" + value.DDESC + "("
                                + depName + ")&nbsp;&nbsp;</label>";
                        } else {
                            str += "<label><input type='checkbox' name='"
                                + monitorType + "code' value='"
                                + value.CCODE + "' descval='"
                                + value.DDESC + "(" + value.DEP
                                + ")'/>" + value.DDESC + "("
                                + depName + ")&nbsp;&nbsp;</label>";
                        }
                    });
                    str += "<label><input  type='checkbox' name='" + monitorType + "code' value='hspstd' descval = '" + descval + "'>" + descval + "&nbsp;&nbsp;</label>";
                    // 添加已经选中的复选框
                    $('#' + tabId + ' #' + monitorType + 'Codes ul').append(str);
                    if (data.length > 5) {
                        var strShowMore = "<div class='doctorPerf' id='showMore" + monitorType + "'><a href='#' style='text-decoration:none'><i class='icon-plus-sign'style='font-size: 25px;'></i></a></div>";
                        // 添加已经选中的复选框
                        $('#' + tabId + ' #' + monitorType + 'Codes').append(strShowMore);
                        var $category = $('#' + tabId + ' #' + monitorType + 'Codes ul label:gt(6)');
                        $category.hide();
                        var $toggleBtn = $('#' + tabId + ' #showMore' + monitorType);
                        $toggleBtn.toggle(function () {
                            $category.show();
                            $(this).find('i').removeClass('icon-plus-sign').addClass('icon-minus-sign').css('color', 'red');
                        }, function () {
                            $category.hide();
                            $(this).find('i')
                                .removeClass('icon-minus-sign')
                                .addClass('icon-plus-sign')
                                .css('color', '#428BCA');
                        });
                    }

                    $('#' + tabId + ' input[name="' + monitorType + 'code"]').live('ifChanged', function () {
                        if ($('#' + tabId + ' input[name="' + monitorType + 'code"]:checked').size() === 0) {
                            $(this).attr('checked', 'checked');
                        }
                        drawRadarBySelectedQuota(monitorType);
                    });
                }
            }
            //初始化icheck
            $('#' + tabId + ' input').iCheck({
                checkboxClass: 'icheckbox_square-blue',
                radioClass: 'iradio_square-blue',
                increaseArea: '10%' // optional
            });

            $('#' + tabId + ' #'+monitorType+' #quotaPanel input').live('ifChanged', function () {
                drawRadarBySelectedQuota(monitorType);
            });

            drawRadarBySelectedQuota(monitorType);
        });
}

/**
 *  @ Discription:初始化弹出框医师表格
 * @Autor ：liuhongjun
 * @param dialog
 * @param notSelectObj
 * @param selectedObj
 * @returns any ywcDGDoctorPerf.js 2017年6月11日上午10:21:31
 */
function loadTable(dialog, notSelectObj, selectedObj) {
    //未被选择的对象
    dialog.find('#notGrantAuthoritiesToUserGrid').html('<table id="notSelectTable" class="display" cellspacing="0" width="100%">'
        + '</table>');
    var columns = [
        {
            "data": "check",
            "render": function (obj) {
                return '<input type="checkbox" name="notSelectObj" value="' + full.ddesc + '" />';
            },
            "className": "center"
        }, {
            "data": "desc",
            "render": function (obj, type, full) {
                return full.ddesc + "(" + full.dep + ")";
            }
        }];
    var paramsDataTables = {scrollY: '300px', lengthChange: false, info: false, ordering: false};
    var table1 = dialog.find('#notSelectTable').dataTableBasic(columns, notSelectObj, paramsDataTables);

    //已被选择的对象
    dialog.find('#grantAuthoritiesToUserGrid').html('<table id="selectedTable" class="display" cellspacing="0" width="100%">'
        + '<thead><tr><th><input type="checkbox" onclick="selectAll(\'grantAuthoritiesToUserGrid\')" /></th><th>选择对象</th></tr></thead></table>');
    var columns = [
        {
            "data": "check",
            "render": function (obj) {
                return '<input type="checkbox" name="selectedObj" value="' + full.ddesc + '" descval="' + full.ddesc + "(" + full.dep + ")" + '"/>';
            },
            "className": "center"
        }, {
            "data": "ddesc",
            "render": function (obj, type, full) {
                return full.ddesc + "(" + full.dep + ")";
            }
        }
    ];
    var paramsDataTables = {scrollY: '300px', lengthChange: false, info: false, ordering: false};
    var table2 = dialog.find('#selectedTable').dataTableBasic(columns, selectedObj, paramsDataTables);
}

checkAll = false;

function selectAll(divId) {
    if (checkAll) {//全不选
        $("#" + divId + " tr input[type='checkbox']").each(function () {
            $(this).removeAttr("checked");//remove
        });
        checkAll = false;
    } else {//全选
        $("#" + divId + " tr input[type='checkbox']").each(function () {

            $(this).attr("checked", "checked");
        });
        checkAll = true;

    }
}

/**
 * 医师得分分析
 * @param {} scoreQuota
 * @param {} monitorType
 * @param {} functionName
 */
function getDoctorScore(scoreQuota, monitorType, functionName, doctorDesc) {
    var columns = [{
        "data": "ddesc", "render": function (obj, type, full) {
            // 	   	   // 如果不属于当前科室
            if (full.isBelongTo == false) {
                return "<span data-toggle=\"tooltip\" data-placement=\"right\" title=\"" + full.currentDepName + "\"><font color=\"red\">" + full.ddesc + "</font></span>";
            } else {
                return full.ddesc;
            }
        },
        "title": doctorDesc
    }, {
        "data": "QuotaValue",
        "title": "综合得分"
    }, {
        "data": "CapacityScore",
        "title": "产能得分"
    }, {
        "data": "EfficiencyScore",
        "title": "效率得分"
    }, {
        "data": "QualityScore",
        "title": "安全得分"
    }];
    var datas = [];
    var ajaxCnt = scoreQuota.length;
    for (var i in scoreQuota) {
        var quotaCode = scoreQuota[i].quotaCode;
        getPerfScoreTable(quotaCode, monitorType,
            function (quotaCode, data) {
                var data_s = [];
                data_s = data_s.concat(data);
                if (datas.length <= 0) {
                    datas = data_s;
                }
                mergeListcc(datas, data_s, 'ccode', '', quotaCode);
                ajaxCnt--;
            });
    }
    var timer = setInterval(function () {
        if (ajaxCnt == 0) {
            clearInterval(timer);
            for (var i in datas) {
                datas[i].QuotaValue = datas[i].TotalScore;
            }
            var tabledata = null;
            $.when(isBelongToDep(monitorType, datas)).done(function () {
                var paramsDataTables = {order:[[1,'desc']],ordering:true};
                tabledata = $("#" + tabId + " #" + monitorType + "-score-table").drgsDataTable(columns, datas, paramsDataTables);
            })
        }
    }, 200);
};

/**
 *
 * @ Discription:
 * @Autor ：liuhongjun
 * @param dialog
 * @param notSelectObj
 * @param selectedObj
 * @returns
 * any
 * ywcDGDoctorPerf.js
 * 2017年6月12日下午2:03:12
 */
function loadDoctorTable(dialog, notSelectObj) {
    //未被选择的对象
    dialog.find('#notGrantAuthoritiesToUserGrid').html('<table id="notSelectTable" class="display" cellspacing="0" width="100%"></table>');
    var columns = [
        {
            "data": "check",
            "render": function (obj) {
                return '<input type="checkbox" name="notSelectObj" value="' + full.ddesc + '" />';
            },
            "className": "center"
        }, {
            "data": "desc",
            "render": function (obj, type, full) {
                return full.ddesc + "(" + full.dep + ")";
            }
        }
    ];
    var paramsDataTables = {scrollY: '300px', lengthChange: false, info: false, ordering: false};
    var table1 = dialog.find('#notSelectTable').dataTableBasic(columns, notSelectObj, paramsDataTables);
}

